<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>字符串类 | 前端文档</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="shortcut icon" href="/icons/favicon.ico">
    <meta name="description" content="前端文档">
    <meta name="theme-color" content="#3eaf7c">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    
    <link rel="preload" href="/assets/css/0.styles.d3d84be6.css" as="style"><link rel="preload" href="/assets/js/app.7f089e4c.js" as="script"><link rel="preload" href="/assets/js/2.9f5e73e8.js" as="script"><link rel="preload" href="/assets/js/3.2a47c23c.js" as="script"><link rel="preload" href="/assets/js/47.bb78b392.js" as="script"><link rel="prefetch" href="/assets/js/10.b5c5700e.js"><link rel="prefetch" href="/assets/js/11.80390338.js"><link rel="prefetch" href="/assets/js/12.f067bd5f.js"><link rel="prefetch" href="/assets/js/13.20c94e2c.js"><link rel="prefetch" href="/assets/js/14.3a03f335.js"><link rel="prefetch" href="/assets/js/15.fc48fa10.js"><link rel="prefetch" href="/assets/js/16.06981385.js"><link rel="prefetch" href="/assets/js/17.9b40465f.js"><link rel="prefetch" href="/assets/js/18.d89ca224.js"><link rel="prefetch" href="/assets/js/19.c6848e16.js"><link rel="prefetch" href="/assets/js/20.a0711b97.js"><link rel="prefetch" href="/assets/js/21.d566ad85.js"><link rel="prefetch" href="/assets/js/22.4901943a.js"><link rel="prefetch" href="/assets/js/23.d6255fa3.js"><link rel="prefetch" href="/assets/js/24.a88ce84e.js"><link rel="prefetch" href="/assets/js/25.19886fd0.js"><link rel="prefetch" href="/assets/js/26.5c944025.js"><link rel="prefetch" href="/assets/js/27.a61fd313.js"><link rel="prefetch" href="/assets/js/28.1e40ebfd.js"><link rel="prefetch" href="/assets/js/29.60c8dbe1.js"><link rel="prefetch" href="/assets/js/30.64963157.js"><link rel="prefetch" href="/assets/js/31.931ea69e.js"><link rel="prefetch" href="/assets/js/32.bce771e5.js"><link rel="prefetch" href="/assets/js/33.e9c7453f.js"><link rel="prefetch" href="/assets/js/34.99c27ee8.js"><link rel="prefetch" href="/assets/js/35.4066b2d3.js"><link rel="prefetch" href="/assets/js/36.fc941c57.js"><link rel="prefetch" href="/assets/js/37.14163d29.js"><link rel="prefetch" href="/assets/js/38.43335765.js"><link rel="prefetch" href="/assets/js/39.156dbace.js"><link rel="prefetch" href="/assets/js/4.6d8feda3.js"><link rel="prefetch" href="/assets/js/40.163bfe43.js"><link rel="prefetch" href="/assets/js/41.df97e701.js"><link rel="prefetch" href="/assets/js/42.9bafe70e.js"><link rel="prefetch" href="/assets/js/43.b86d1037.js"><link rel="prefetch" href="/assets/js/44.fc145987.js"><link rel="prefetch" href="/assets/js/45.594b749a.js"><link rel="prefetch" href="/assets/js/46.b6c8de53.js"><link rel="prefetch" href="/assets/js/48.3627a180.js"><link rel="prefetch" href="/assets/js/49.bbd39edf.js"><link rel="prefetch" href="/assets/js/5.77776c33.js"><link rel="prefetch" href="/assets/js/50.b68a715f.js"><link rel="prefetch" href="/assets/js/51.bc4475a2.js"><link rel="prefetch" href="/assets/js/6.480ee688.js"><link rel="prefetch" href="/assets/js/7.92c95b3e.js"><link rel="prefetch" href="/assets/js/8.4389b91c.js"><link rel="prefetch" href="/assets/js/9.4470fdec.js">
    <link rel="stylesheet" href="/assets/css/0.styles.d3d84be6.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">前端文档</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="standard Menu" class="dropdown-title"><span class="title">规范</span> <span class="arrow down"></span></button> <button type="button" aria-label="standard Menu" class="mobile-dropdown-title"><span class="title">规范</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/standard/HTML/" class="nav-link">
  HTML编码规范
</a></li><li class="dropdown-item"><!----> <a href="/standard/CSS/" class="nav-link">
  CSS编码规范
</a></li><li class="dropdown-item"><!----> <a href="/standard/JavaScript/" class="nav-link">
  JavaScript编码规范
</a></li><li class="dropdown-item"><!----> <a href="/standard/VUE/" class="nav-link">
  VUE编码规范
</a></li><li class="dropdown-item"><!----> <a href="/standard/Markdown/" class="nav-link">
  Markdown语法说明
</a></li><li class="dropdown-item"><!----> <a href="/standard/Commit/" class="nav-link">
  Git提交规范
</a></li><li class="dropdown-item"><!----> <a href="/standard/JSON/" class="nav-link">
  JSON数据规范
</a></li><li class="dropdown-item"><!----> <a href="/standard/CopyWriter/" class="nav-link">
  文案风格指南
</a></li></ul></div></div><div class="nav-item"><a href="/desgin/" class="nav-link">
  可视化平台
</a></div><div class="nav-item"><a href="/materiel/" class="nav-link">
  物料
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="plugin Menu" class="dropdown-title"><span class="title">插件</span> <span class="arrow down"></span></button> <button type="button" aria-label="plugin Menu" class="mobile-dropdown-title"><span class="title">插件</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/plugin/HRM-Player/" class="nav-link">
  HRM 播放器
</a></li><li class="dropdown-item"><!----> <a href="/plugin/Vue-Flv-Player/" class="nav-link">
  FLV 播放器
</a></li><li class="dropdown-item"><!----> <a href="/plugin/Calendar/" class="nav-link">
  Calendar 农历面板
</a></li><li class="dropdown-item"><!----> <a href="/plugin/Very-Axios/" class="nav-link">
  Very-Axios
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="utils Menu" class="dropdown-title"><span class="title">函数库</span> <span class="arrow down"></span></button> <button type="button" aria-label="utils Menu" class="mobile-dropdown-title"><span class="title">函数库</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/utils/Type/" class="nav-link">
  类型判断类
</a></li><li class="dropdown-item"><!----> <a href="/utils/Validator/" class="nav-link">
  常规验证类
</a></li><li class="dropdown-item"><!----> <a href="/utils/String/" class="nav-link">
  字符串类
</a></li><li class="dropdown-item"><!----> <a href="/utils/Array/" class="nav-link">
  数组类
</a></li><li class="dropdown-item"><!----> <a href="/utils/Number/" class="nav-link">
  数字类
</a></li><li class="dropdown-item"><!----> <a href="/utils/Money/" class="nav-link">
  金额类
</a></li><li class="dropdown-item"><!----> <a href="/utils/Date/" class="nav-link">
  日期类
</a></li><li class="dropdown-item"><!----> <a href="/utils/Calculate/" class="nav-link">
  浮点计算类
</a></li><li class="dropdown-item"><!----> <a href="/utils/Tools/" class="nav-link">
  工具类
</a></li><li class="dropdown-item"><!----> <a href="/utils/Storage/" class="nav-link">
  存储类
</a></li><li class="dropdown-item"><!----> <a href="/utils/DOM/" class="nav-link">
  DOM类
</a></li><li class="dropdown-item"><!----> <a href="/utils/Event/" class="nav-link">
  事件处理类
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="tools Menu" class="dropdown-title"><span class="title">工具</span> <span class="arrow down"></span></button> <button type="button" aria-label="tools Menu" class="mobile-dropdown-title"><span class="title">工具</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/tools/NodeJS/" class="nav-link">
  NodeJS
</a></li><li class="dropdown-item"><!----> <a href="/tools/Sourcetree/" class="nav-link">
  Sourcetree
</a></li><li class="dropdown-item"><!----> <a href="/tools/VisualStudioCode/" class="nav-link">
  VSCode
</a></li><li class="dropdown-item"><!----> <a href="/tools/XShell/" class="nav-link">
  XShell
</a></li><li class="dropdown-item"><!----> <a href="/tools/Typora/" class="nav-link">
  Typora
</a></li><li class="dropdown-item"><!----> <a href="/tools/PicGo/" class="nav-link">
  PicGo
</a></li><li class="dropdown-item"><!----> <a href="/tools/XMind/" class="nav-link">
  XMind
</a></li></ul></div></div> <!----></nav> <label title="切换暗色" class="model"><input type="checkbox" hidden="hidden" name="model" id>⚫</label></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="standard Menu" class="dropdown-title"><span class="title">规范</span> <span class="arrow down"></span></button> <button type="button" aria-label="standard Menu" class="mobile-dropdown-title"><span class="title">规范</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/standard/HTML/" class="nav-link">
  HTML编码规范
</a></li><li class="dropdown-item"><!----> <a href="/standard/CSS/" class="nav-link">
  CSS编码规范
</a></li><li class="dropdown-item"><!----> <a href="/standard/JavaScript/" class="nav-link">
  JavaScript编码规范
</a></li><li class="dropdown-item"><!----> <a href="/standard/VUE/" class="nav-link">
  VUE编码规范
</a></li><li class="dropdown-item"><!----> <a href="/standard/Markdown/" class="nav-link">
  Markdown语法说明
</a></li><li class="dropdown-item"><!----> <a href="/standard/Commit/" class="nav-link">
  Git提交规范
</a></li><li class="dropdown-item"><!----> <a href="/standard/JSON/" class="nav-link">
  JSON数据规范
</a></li><li class="dropdown-item"><!----> <a href="/standard/CopyWriter/" class="nav-link">
  文案风格指南
</a></li></ul></div></div><div class="nav-item"><a href="/desgin/" class="nav-link">
  可视化平台
</a></div><div class="nav-item"><a href="/materiel/" class="nav-link">
  物料
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="plugin Menu" class="dropdown-title"><span class="title">插件</span> <span class="arrow down"></span></button> <button type="button" aria-label="plugin Menu" class="mobile-dropdown-title"><span class="title">插件</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/plugin/HRM-Player/" class="nav-link">
  HRM 播放器
</a></li><li class="dropdown-item"><!----> <a href="/plugin/Vue-Flv-Player/" class="nav-link">
  FLV 播放器
</a></li><li class="dropdown-item"><!----> <a href="/plugin/Calendar/" class="nav-link">
  Calendar 农历面板
</a></li><li class="dropdown-item"><!----> <a href="/plugin/Very-Axios/" class="nav-link">
  Very-Axios
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="utils Menu" class="dropdown-title"><span class="title">函数库</span> <span class="arrow down"></span></button> <button type="button" aria-label="utils Menu" class="mobile-dropdown-title"><span class="title">函数库</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/utils/Type/" class="nav-link">
  类型判断类
</a></li><li class="dropdown-item"><!----> <a href="/utils/Validator/" class="nav-link">
  常规验证类
</a></li><li class="dropdown-item"><!----> <a href="/utils/String/" class="nav-link">
  字符串类
</a></li><li class="dropdown-item"><!----> <a href="/utils/Array/" class="nav-link">
  数组类
</a></li><li class="dropdown-item"><!----> <a href="/utils/Number/" class="nav-link">
  数字类
</a></li><li class="dropdown-item"><!----> <a href="/utils/Money/" class="nav-link">
  金额类
</a></li><li class="dropdown-item"><!----> <a href="/utils/Date/" class="nav-link">
  日期类
</a></li><li class="dropdown-item"><!----> <a href="/utils/Calculate/" class="nav-link">
  浮点计算类
</a></li><li class="dropdown-item"><!----> <a href="/utils/Tools/" class="nav-link">
  工具类
</a></li><li class="dropdown-item"><!----> <a href="/utils/Storage/" class="nav-link">
  存储类
</a></li><li class="dropdown-item"><!----> <a href="/utils/DOM/" class="nav-link">
  DOM类
</a></li><li class="dropdown-item"><!----> <a href="/utils/Event/" class="nav-link">
  事件处理类
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="tools Menu" class="dropdown-title"><span class="title">工具</span> <span class="arrow down"></span></button> <button type="button" aria-label="tools Menu" class="mobile-dropdown-title"><span class="title">工具</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/tools/NodeJS/" class="nav-link">
  NodeJS
</a></li><li class="dropdown-item"><!----> <a href="/tools/Sourcetree/" class="nav-link">
  Sourcetree
</a></li><li class="dropdown-item"><!----> <a href="/tools/VisualStudioCode/" class="nav-link">
  VSCode
</a></li><li class="dropdown-item"><!----> <a href="/tools/XShell/" class="nav-link">
  XShell
</a></li><li class="dropdown-item"><!----> <a href="/tools/Typora/" class="nav-link">
  Typora
</a></li><li class="dropdown-item"><!----> <a href="/tools/PicGo/" class="nav-link">
  PicGo
</a></li><li class="dropdown-item"><!----> <a href="/tools/XMind/" class="nav-link">
  XMind
</a></li></ul></div></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>函数库</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/utils/" aria-current="page" class="sidebar-link">Cafe-Utils</a></li><li><a href="/utils/Type.html" class="sidebar-link">类型判断类</a></li><li><a href="/utils/Validator.html" class="sidebar-link">常规验证类</a></li><li><a href="/utils/String.html" aria-current="page" class="active sidebar-link">字符串类</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/utils/String.html#formatstr" class="sidebar-link">formatStr</a></li><li class="sidebar-sub-header"><a href="/utils/String.html#tocamelcase" class="sidebar-link">toCamelCase</a></li><li class="sidebar-sub-header"><a href="/utils/String.html#fromcamelcase" class="sidebar-link">fromCamelCase</a></li><li class="sidebar-sub-header"><a href="/utils/String.html#trim" class="sidebar-link">trim</a></li><li class="sidebar-sub-header"><a href="/utils/String.html#escapehtml" class="sidebar-link">escapeHTML</a></li><li class="sidebar-sub-header"><a href="/utils/String.html#unescapehtml" class="sidebar-link">unescapeHTML</a></li><li class="sidebar-sub-header"><a href="/utils/String.html#striphtmltags" class="sidebar-link">stripHTMLTags</a></li><li class="sidebar-sub-header"><a href="/utils/String.html#subgbstring" class="sidebar-link">subGbString</a></li><li class="sidebar-sub-header"><a href="/utils/String.html#getgblen" class="sidebar-link">getGbLen</a></li><li class="sidebar-sub-header"><a href="/utils/String.html#changecase" class="sidebar-link">changeCase</a></li></ul></li><li><a href="/utils/Array.html" class="sidebar-link">数组类</a></li><li><a href="/utils/Number.html" class="sidebar-link">数字类</a></li><li><a href="/utils/Money.html" class="sidebar-link">金额类</a></li><li><a href="/utils/Date.html" class="sidebar-link">日期类</a></li><li><a href="/utils/Calculate.html" class="sidebar-link">浮点计算类</a></li><li><a href="/utils/Tools.html" class="sidebar-link">工具类</a></li><li><a href="/utils/Storage.html" class="sidebar-link">存储类</a></li><li><a href="/utils/DOM.html" class="sidebar-link">DOM类</a></li><li><a href="/utils/Event.html" class="sidebar-link">事件处理类</a></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="字符串类"><a href="#字符串类" class="header-anchor">#</a> 字符串类</h1> <h2 id="formatstr"><a href="#formatstr" class="header-anchor">#</a> formatStr</h2> <p>按规则格式化。</p> <h3 id="源码"><a href="#源码" class="header-anchor">#</a> 源码：</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">/**
 * @description 格式化字符串
 * @param {string} str 字符串
 * @param {number} len 位数
 * @param {string} char - 格式化字符
 * @returns {string}
 */</span>
<span class="token keyword">const</span> <span class="token function-variable function">formatStr</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">str<span class="token punctuation">,</span> len<span class="token punctuation">,</span> char</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">let</span> reg <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">RegExp</span><span class="token punctuation">(</span><span class="token string">&quot;\\d{1,&quot;</span> <span class="token operator">+</span> len <span class="token operator">+</span> <span class="token string">&quot;}&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;g&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">return</span> str<span class="token punctuation">.</span><span class="token function">match</span><span class="token punctuation">(</span>reg<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span>char<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><h3 id="使用"><a href="#使用" class="header-anchor">#</a> 使用：</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function">formatStr</span><span class="token punctuation">(</span><span class="token string">&quot;999999999&quot;</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token string">&quot;-&quot;</span><span class="token punctuation">)</span> <span class="token comment">// 999-999-999</span>
<span class="token function">formatStr</span><span class="token punctuation">(</span><span class="token string">&quot;abcdefg&quot;</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token string">&quot;-&quot;</span><span class="token punctuation">)</span> <span class="token comment">// abcd-efg</span>
</code></pre></div><h2 id="tocamelcase"><a href="#tocamelcase" class="header-anchor">#</a> toCamelCase</h2> <p>字符串转换为驼峰。</p> <h3 id="源码-2"><a href="#源码-2" class="header-anchor">#</a> 源码：</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">/**
 * @description 字符串转换为驼峰
 * @param {string} str 字符串
 * @returns {string}
 */</span>
<span class="token keyword">const</span> <span class="token function-variable function">toCamelCase</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">str</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> str<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">^([A-Z])|[\s-_]+(\w)</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">match<span class="token punctuation">,</span> p1<span class="token punctuation">,</span> p2<span class="token punctuation">,</span> offset</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span>  p2 <span class="token operator">?</span> p2<span class="token punctuation">.</span><span class="token function">toUpperCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">:</span> p1<span class="token punctuation">.</span><span class="token function">toLowerCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><h3 id="使用-2"><a href="#使用-2" class="header-anchor">#</a> 使用：</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function">toCamelCase</span><span class="token punctuation">(</span><span class="token string">&quot;some_database_field_name&quot;</span><span class="token punctuation">)</span> <span class="token comment">// &quot;someDatabaseFieldName&quot;</span>
<span class="token function">toCamelCase</span><span class="token punctuation">(</span><span class="token string">&quot;Some label that needs to be camelized&quot;</span><span class="token punctuation">)</span> <span class="token comment">// &quot;someLabelThatNeedsToBeCamelized&quot;</span>
<span class="token function">toCamelCase</span><span class="token punctuation">(</span><span class="token string">&quot;some-javascript-property&quot;</span><span class="token punctuation">)</span> <span class="token comment">// &quot;someJavascriptProperty&quot;</span>
<span class="token function">toCamelCase</span><span class="token punctuation">(</span><span class="token string">&quot;some-mixed_string with spaces_underscores-and-hyphens&quot;</span><span class="token punctuation">)</span> <span class="token comment">// &quot;someMixedStringWithSpacesUnderscoresAndHyphens&quot;</span>
</code></pre></div><h2 id="fromcamelcase"><a href="#fromcamelcase" class="header-anchor">#</a> fromCamelCase</h2> <p>驼峰转字符串。</p> <h3 id="源码-3"><a href="#源码-3" class="header-anchor">#</a> 源码：</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">/**
 * @description 驼峰转字符串
 * @param {string} str 字符串
 * @param {string} separator 切割符
 * @returns {string}
 */</span>
<span class="token keyword">const</span> fromCamelCase <span class="token operator">=</span> <span class="token punctuation">(</span>str<span class="token punctuation">,</span> separator <span class="token operator">=</span> <span class="token string">'_'</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> str<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">([a-z\d])([A-Z])</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">,</span> <span class="token string">'$1'</span> <span class="token operator">+</span> separator <span class="token operator">+</span> <span class="token string">'$2'</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">([A-Z]+)([A-Z][a-z\d]+)</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">,</span> <span class="token string">'$1'</span> <span class="token operator">+</span> separator <span class="token operator">+</span> <span class="token string">'$2'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toLowerCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><h3 id="使用-3"><a href="#使用-3" class="header-anchor">#</a> 使用：</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function">fromCamelCase</span><span class="token punctuation">(</span><span class="token string">'someDatabaseFieldName'</span><span class="token punctuation">,</span> <span class="token string">' '</span><span class="token punctuation">)</span> <span class="token comment">// &quot;some database field name&quot;</span>
<span class="token function">fromCamelCase</span><span class="token punctuation">(</span><span class="token string">'someLabelThatNeedsToBeCamelized'</span><span class="token punctuation">,</span> <span class="token string">'-'</span><span class="token punctuation">)</span> <span class="token comment">// &quot;some-label-that-needs-to-be-camelized&quot;</span>
<span class="token function">fromCamelCase</span><span class="token punctuation">(</span><span class="token string">'someJavascriptProperty'</span><span class="token punctuation">)</span> <span class="token comment">// &quot;some_javascript_property&quot;</span>
</code></pre></div><h2 id="trim"><a href="#trim" class="header-anchor">#</a> trim</h2> <p>去除空格，。</p> <h3 id="源码-4"><a href="#源码-4" class="header-anchor">#</a> 源码：</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">/**
 * @description 去除空格
 * @param {string} str 字符串
 * @param {number} type 1-所有空格  2-前后空格  3-前空格 4-后空格
 * @returns {string}
 */</span>
<span class="token keyword">const</span> <span class="token function-variable function">trim</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">str<span class="token punctuation">,</span> type</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  type <span class="token operator">=</span> type <span class="token operator">||</span> <span class="token number">1</span><span class="token punctuation">;</span>
  <span class="token keyword">switch</span> <span class="token punctuation">(</span>type<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">case</span> <span class="token number">1</span><span class="token operator">:</span>
      <span class="token keyword">return</span> str<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">\s+</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">,</span> <span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">case</span> <span class="token number">2</span><span class="token operator">:</span>
      <span class="token keyword">return</span> str<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">(^\s*)|(\s*$)</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">,</span> <span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">case</span> <span class="token number">3</span><span class="token operator">:</span>
      <span class="token keyword">return</span> str<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">(^\s*)</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">,</span> <span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">case</span> <span class="token number">4</span><span class="token operator">:</span>
      <span class="token keyword">return</span> str<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">(\s*$)</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">,</span> <span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">default</span><span class="token operator">:</span>
      <span class="token keyword">return</span> str<span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><h3 id="使用-4"><a href="#使用-4" class="header-anchor">#</a> 使用：</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">let</span> str <span class="token operator">=</span> <span class="token string">&quot; abc de f &quot;</span>

<span class="token function">trim</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span>   <span class="token comment">// &quot;abcdef&quot;</span>
<span class="token function">trim</span><span class="token punctuation">(</span>str<span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment">// &quot;abcdef&quot;</span>
<span class="token function">trim</span><span class="token punctuation">(</span>str<span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment">// &quot;abc de f&quot;</span>
<span class="token function">trim</span><span class="token punctuation">(</span>str<span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">)</span> <span class="token comment">// &quot;abc de f &quot;</span>
<span class="token function">trim</span><span class="token punctuation">(</span>str<span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">)</span> <span class="token comment">// &quot; abc de f&quot;</span>
</code></pre></div><h2 id="escapehtml"><a href="#escapehtml" class="header-anchor">#</a> escapeHTML</h2> <p>转义 HTML 字符串。</p> <h3 id="源码-5"><a href="#源码-5" class="header-anchor">#</a> 源码：</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">/**
 * @description 转义HTML字符串
 * @param {string} str 字符串
 * @returns {string}
 */</span>
<span class="token keyword">const</span> <span class="token function-variable function">escapeHTML</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">str</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> str<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span>
    <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">[&amp;&lt;&gt;'&quot;]</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">,</span>
    <span class="token parameter">tag</span> <span class="token operator">=&gt;</span>
      <span class="token punctuation">(</span><span class="token punctuation">{</span>
        <span class="token string">'&amp;'</span><span class="token operator">:</span> <span class="token string">'&amp;amp;'</span><span class="token punctuation">,</span>
        <span class="token string">'&lt;'</span><span class="token operator">:</span> <span class="token string">'&amp;lt;'</span><span class="token punctuation">,</span>
        <span class="token string">'&gt;'</span><span class="token operator">:</span> <span class="token string">'&amp;gt;'</span><span class="token punctuation">,</span>
        <span class="token string">&quot;'&quot;</span><span class="token operator">:</span> <span class="token string">'&amp;#39;'</span><span class="token punctuation">,</span>
        <span class="token string">'&quot;'</span><span class="token operator">:</span> <span class="token string">'&amp;quot;'</span>
      <span class="token punctuation">}</span><span class="token punctuation">[</span>tag<span class="token punctuation">]</span> <span class="token operator">||</span> tag<span class="token punctuation">)</span>
  <span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><h3 id="使用-5"><a href="#使用-5" class="header-anchor">#</a> 使用：</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function">escapeHTML</span><span class="token punctuation">(</span><span class="token string">'&lt;a href=&quot;#&quot;&gt;Me &amp; you&lt;/a&gt;'</span><span class="token punctuation">)</span> <span class="token comment">// &quot;&amp;lt;a href=&amp;quot;#&amp;quot;&amp;gt;Me &amp;amp; you&amp;lt;/a&amp;gt;&quot;</span>
</code></pre></div><h2 id="unescapehtml"><a href="#unescapehtml" class="header-anchor">#</a> unescapeHTML</h2> <p>反转义 HTML。</p> <h3 id="源码-6"><a href="#源码-6" class="header-anchor">#</a> 源码：</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">/**
 * @description 反转 escape
 * @param {string} str 字符串
 * @returns {string}
 */</span>
<span class="token keyword">const</span> <span class="token function-variable function">unescapeHTML</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">str</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> str<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span>
    <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">&amp;amp;|&amp;lt;|&amp;gt;|&amp;#39;|&amp;quot;</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">,</span>
    <span class="token parameter">tag</span> <span class="token operator">=&gt;</span>
      <span class="token punctuation">(</span><span class="token punctuation">{</span>
        <span class="token string">'&amp;amp;'</span><span class="token operator">:</span> <span class="token string">'&amp;'</span><span class="token punctuation">,</span>
        <span class="token string">'&amp;lt;'</span><span class="token operator">:</span> <span class="token string">'&lt;'</span><span class="token punctuation">,</span>
        <span class="token string">'&amp;gt;'</span><span class="token operator">:</span> <span class="token string">'&gt;'</span><span class="token punctuation">,</span>
        <span class="token string">'&amp;#39;'</span><span class="token operator">:</span> <span class="token string">&quot;'&quot;</span><span class="token punctuation">,</span>
        <span class="token string">'&amp;quot;'</span><span class="token operator">:</span> <span class="token string">'&quot;'</span>
      <span class="token punctuation">}</span><span class="token punctuation">[</span>tag<span class="token punctuation">]</span> <span class="token operator">||</span> tag<span class="token punctuation">)</span>
  <span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><h3 id="使用-6"><a href="#使用-6" class="header-anchor">#</a> 使用：</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function">unescapeHTML</span><span class="token punctuation">(</span><span class="token string">'&amp;lt;a href=&amp;quot;#&amp;quot;&amp;gt;Me &amp;amp; you&amp;lt;/a&amp;gt;'</span><span class="token punctuation">)</span> <span class="token comment">// '&lt;a href=&quot;#&quot;&gt;Me &amp; you&lt;/a&gt;'</span>
</code></pre></div><h2 id="striphtmltags"><a href="#striphtmltags" class="header-anchor">#</a> stripHTMLTags</h2> <p>过滤 html 标签。</p> <h3 id="源码-7"><a href="#源码-7" class="header-anchor">#</a> 源码：</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">/**
 * @description 过滤html标签
 * @export
 * @param {string} str 字符串
 * @returns {string}
 */</span>
<span class="token keyword">const</span> <span class="token function-variable function">stripHTMLTags</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">str</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> str<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">&lt;[^&gt;]*&gt;</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">,</span> <span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><h3 id="使用-7"><a href="#使用-7" class="header-anchor">#</a> 使用：</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function">stripHTMLTags</span><span class="token punctuation">(</span><span class="token string">&quot;&lt;p&gt;&lt;em&gt;wang&lt;/em&gt; &lt;strong&gt;daodao&lt;/strong&gt;&lt;/p&gt;&quot;</span><span class="token punctuation">)</span> <span class="token comment">// &quot;wang daodao&quot;</span>
</code></pre></div><h2 id="subgbstring"><a href="#subgbstring" class="header-anchor">#</a> subGbString</h2> <p>截取字符，区分中英文，中文占两字符，空格占一字符。</p> <h3 id="源码-8"><a href="#源码-8" class="header-anchor">#</a> 源码：</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">/**
 * @description 截取字符，区分中英文，中文两字符，空格一字符
 * @param {String} str - 字符串
 * @param {Number} len - 字符串截取长度
 * @returns {String} - 返回截取后字符
 */</span>
<span class="token keyword">const</span> <span class="token function-variable function">subGbString</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">str<span class="token punctuation">,</span> len</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">const</span> regexp <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">[^\x00-\xff]</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span> <span class="token comment">// 正则表达式匹配中文</span>
  <span class="token comment">// 当字符串字节长度小于指定的字节长度时</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>str<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span>regexp<span class="token punctuation">,</span> <span class="token string">'aa'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>length <span class="token operator">&lt;=</span> len<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> str
  <span class="token punctuation">}</span>
  <span class="token comment">// 假设指定长度内都是中文</span>
  <span class="token keyword">const</span> m <span class="token operator">=</span> Math<span class="token punctuation">.</span><span class="token function">floor</span><span class="token punctuation">(</span>len <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">)</span>
  <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">let</span> i <span class="token operator">=</span> m<span class="token punctuation">,</span> j <span class="token operator">=</span> str<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i <span class="token operator">&lt;</span> j<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// 当截取字符串字节长度满足指定的字节长度</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>str<span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> i<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span>regexp<span class="token punctuation">,</span> <span class="token string">'aa'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>length <span class="token operator">&gt;=</span> len<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">return</span> str<span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> i<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><h3 id="使用-8"><a href="#使用-8" class="header-anchor">#</a> 使用：</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">let</span> str1 <span class="token operator">=</span> <span class="token string">&quot;截取字符，区分中英文&quot;</span>
<span class="token keyword">let</span> str2 <span class="token operator">=</span> <span class="token string">&quot;subGbString&quot;</span>
<span class="token keyword">let</span> str3 <span class="token operator">=</span> <span class="token string">&quot;subGbString 截取字符，区分中英文&quot;</span>

<span class="token function">subGbString</span><span class="token punctuation">(</span>str1<span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">)</span>   <span class="token comment">// &quot;截取&quot;</span>
<span class="token function">subGbString</span><span class="token punctuation">(</span>str1<span class="token punctuation">,</span><span class="token number">100</span><span class="token punctuation">)</span> <span class="token comment">// &quot;截取字符，区分中英文&quot;</span>
<span class="token function">subGbString</span><span class="token punctuation">(</span>str2<span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">)</span>   <span class="token comment">// &quot;subG&quot;</span>
<span class="token function">subGbString</span><span class="token punctuation">(</span>str3<span class="token punctuation">,</span><span class="token number">14</span><span class="token punctuation">)</span>  <span class="token comment">// &quot;subGbString 截&quot;</span>
</code></pre></div><h2 id="getgblen"><a href="#getgblen" class="header-anchor">#</a> getGbLen</h2> <p>判断字符长度，区分中英文，包括空格。</p> <h3 id="源码-9"><a href="#源码-9" class="header-anchor">#</a> 源码：</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">/**
 * @description 判断字符长度，区分中英文，包括空格
 * @param {String} str - 字符串
 * @returns {Number} - 返回字符长度
 */</span>
<span class="token keyword">const</span> <span class="token function-variable function">getGbLen</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">str</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">let</span> len <span class="token operator">=</span> <span class="token number">0</span>
  <span class="token keyword">const</span> content <span class="token operator">=</span> str <span class="token operator">||</span> <span class="token string">''</span>
  <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">let</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> content<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>content<span class="token punctuation">.</span><span class="token function">charCodeAt</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span> <span class="token operator">&gt;</span> <span class="token number">127</span> <span class="token operator">||</span> content<span class="token punctuation">.</span><span class="token function">charCodeAt</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span> <span class="token operator">===</span> <span class="token number">94</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      len <span class="token operator">+=</span> <span class="token number">2</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
      len<span class="token operator">++</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
  <span class="token keyword">return</span> len
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><h3 id="使用-9"><a href="#使用-9" class="header-anchor">#</a> 使用：</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">let</span> str1 <span class="token operator">=</span> <span class="token string">&quot;判断字符长度&quot;</span>
<span class="token keyword">let</span> str2 <span class="token operator">=</span> <span class="token string">&quot;getGbLen&quot;</span>
<span class="token keyword">let</span> str3 <span class="token operator">=</span> <span class="token string">&quot;getGbLen 判断字符长度，区分中英文&quot;</span>

<span class="token function">getGbLen</span><span class="token punctuation">(</span>str1<span class="token punctuation">)</span> <span class="token comment">// 12</span>
<span class="token function">getGbLen</span><span class="token punctuation">(</span>str2<span class="token punctuation">)</span> <span class="token comment">// 8</span>
<span class="token function">getGbLen</span><span class="token punctuation">(</span>str3<span class="token punctuation">)</span> <span class="token comment">// 33</span>
</code></pre></div><h2 id="changecase"><a href="#changecase" class="header-anchor">#</a> changeCase</h2> <p>字符串写法转换。</p> <h3 id="源码-10"><a href="#源码-10" class="header-anchor">#</a> 源码：</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">/**
 * @description 字符串写法转换
 * @param {string} str 字符串
 * @param {number} type 1:首字母大写其他小写  2：首页母小写其他大写  3：大小写转换  4：全部大写  5：全部小写
 * @returns {string}
 */</span>
<span class="token keyword">const</span> <span class="token function-variable function">changeCase</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">str<span class="token punctuation">,</span> type</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  type <span class="token operator">=</span> type <span class="token operator">||</span> <span class="token number">4</span><span class="token punctuation">;</span>
  <span class="token keyword">switch</span> <span class="token punctuation">(</span>type<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">case</span> <span class="token number">1</span><span class="token operator">:</span>
      <span class="token keyword">return</span> str<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">\b\w+\b</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">word</span><span class="token punctuation">)</span><span class="token operator">:</span> string <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token punctuation">(</span>
          word<span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toUpperCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span>
          word<span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toLowerCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">case</span> <span class="token number">2</span><span class="token operator">:</span>
      <span class="token keyword">return</span> str<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">\b\w+\b</span><span class="token regex-delimiter">/</span><span class="token regex-flags">g</span></span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">word</span><span class="token punctuation">)</span><span class="token operator">:</span> string <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token punctuation">(</span>
          word<span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toLowerCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span>
          word<span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toUpperCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">case</span> <span class="token number">3</span><span class="token operator">:</span>
      <span class="token keyword">return</span> str
        <span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">word</span><span class="token punctuation">)</span><span class="token operator">:</span> string <span class="token punctuation">{</span>
          <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">[a-z]</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span>word<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> word<span class="token punctuation">.</span><span class="token function">toUpperCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
          <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> word<span class="token punctuation">.</span><span class="token function">toLowerCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
          <span class="token punctuation">}</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span>
        <span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">case</span> <span class="token number">4</span><span class="token operator">:</span>
      <span class="token keyword">return</span> str<span class="token punctuation">.</span><span class="token function">toUpperCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">case</span> <span class="token number">5</span><span class="token operator">:</span>
      <span class="token keyword">return</span> str<span class="token punctuation">.</span><span class="token function">toLowerCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">default</span><span class="token operator">:</span>
      <span class="token keyword">return</span> str<span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><h3 id="使用-10"><a href="#使用-10" class="header-anchor">#</a> 使用：</h3> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">let</span> str1 <span class="token operator">=</span> <span class="token string">&quot;aBcdEf&quot;</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> str2 <span class="token operator">=</span> <span class="token string">&quot;AbcdEf&quot;</span><span class="token punctuation">;</span>

<span class="token function">changeCase</span><span class="token punctuation">(</span>str1<span class="token punctuation">)</span>   <span class="token comment">// &quot;ABCDEF&quot;</span>
<span class="token function">changeCase</span><span class="token punctuation">(</span>str1<span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment">// &quot;Abcdef&quot;</span>
<span class="token function">changeCase</span><span class="token punctuation">(</span>str2<span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment">// &quot;aBCDEF&quot;</span>
<span class="token function">changeCase</span><span class="token punctuation">(</span>str1<span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">)</span> <span class="token comment">// &quot;AbCDeF&quot;</span>
<span class="token function">changeCase</span><span class="token punctuation">(</span>str1<span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">)</span> <span class="token comment">// &quot;ABCDEF&quot;</span>
<span class="token function">changeCase</span><span class="token punctuation">(</span>str1<span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">)</span> <span class="token comment">// &quot;abcdef&quot;</span>
</code></pre></div></div> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">文档更新时间:</span> <span class="time">2021-06-10 17:57:34</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/utils/Validator.html" class="prev">
        常规验证类
      </a></span> <span class="next"><a href="/utils/Array.html">
        数组类
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"><!----><!----></div></div>
    <script src="/assets/js/app.7f089e4c.js" defer></script><script src="/assets/js/2.9f5e73e8.js" defer></script><script src="/assets/js/3.2a47c23c.js" defer></script><script src="/assets/js/47.bb78b392.js" defer></script>
  </body>
</html>
